package org.xfy.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.base.service.impl.BaseServiceImpl;
import org.xfy.org.domain.Department;
import org.xfy.org.exception.BusinessException;
import org.xfy.org.mapper.DepartmentMapper;
import org.xfy.org.service.IDepartmentService;
import org.xfy.base.util.ErrorCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private static String TREE_NAME="deptstree";
    @Override
    @Transactional
    public void add(Department department) {
        redisTemplate.delete(TREE_NAME);
        departmentMapper.add(department);
        if (department.getParentId() != null) {
            //找到父集
            Department department1 = departmentMapper.findOne(department.getParentId());
            String dirPath = department1.getDirPath();
            department.setDirPath(dirPath + "/" + department.getId());
            departmentMapper.update(department);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        redisTemplate.delete(TREE_NAME);
        Department department = departmentMapper.findOne(id);
        //不是最后一级部门
        List<Department> departments = departmentMapper.findByPid(department.getId());
        if (departments.size() > 0) {
            throw new RuntimeException(ErrorCode.Besiness_ERROR.getCode());
        }
        departmentMapper.del(id);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        redisTemplate.delete(TREE_NAME);
        for (int i = 0; i < ids.length; i++) {
            Department department = departmentMapper.findOne(ids[i]);
            //不是最后一级部门
            List<Department> departments = departmentMapper.findByPid(department.getId());
            if (departments.size() > 0) {
                throw new RuntimeException(ErrorCode.Besiness_ERROR.getCode());
            }
        }
        departmentMapper.patchDel(ids);
    }

    @Override
    @Transactional
    public void update(Department department) {
        redisTemplate.delete(TREE_NAME);
        Department department2 = departmentMapper.findOne(department.getId());
        if (!department2.getParentId().equals(department.getParentId())) { //不是最后一级部门
            List<Department> departments = departmentMapper.findByPid(department.getId());
            if (departments.size() > 0) {
                throw new BusinessException(ErrorCode.Besiness_ERROR.getCode());
            }
        }
        if (department.getParentId() != null) {
            //找到父集
            Department department1 = departmentMapper.findOne(department.getParentId());
            String dirPath = department1.getDirPath();
            department.setDirPath(dirPath + "/" + department.getId());
            departmentMapper.update(department);
        }
        redisTemplate.delete(TREE_NAME);
    }

    @Override
    @Cacheable(cacheNames = "dept",key = "'deptstess'")
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }

    @Override
    public List<Department> deptsTree() {
        List<Department> o= (List<Department> )redisTemplate.opsForValue().get(TREE_NAME);
        if(o==null){
            //查询所有的部门
            List<Department> departments = departmentMapper.findAll();
            List<Department> top = new ArrayList<>();
            Map<Long, Department> map = new HashMap<>();
            //将所有集合放到Map中，以他们的id为key
            for (Department department : departments) {
                map.put(department.getId(), department);
            }
            for (Department department : departments) {
                //Pid为空就代表为顶层
                if (department.getParentId() == null) {
                    top.add(department);
                } else {
                    //找到了他的上级
                    Department department1 = map.get(department.getParentId());
                    //将自己放进去
                    department1.getChildren().add(department);
                }
            }
            redisTemplate.opsForValue().set(TREE_NAME,top);
            return top;
        }
        return o;
    }
}
